part of 'page.dart';

class _SingleCollectionPageWlocStateFactory {
  const _SingleCollectionPageWlocStateFactory._internal();

  static const _instance = _SingleCollectionPageWlocStateFactory._internal();

  factory _SingleCollectionPageWlocStateFactory() {
    return _instance;
  }

  _SingleCollectionPageWlocState create({
    required bloc.SingleCollectionPageBlocState blocState,
  }) {
    switch (blocState) {
      case bloc.DefaultState():
        final collectionLexicalItemModels = blocState.collectionLexicalItems.map((lexicalItem) {
          final progressModel = LexicalItemProgressModelFactory().create(
            lexicalItem: lexicalItem,
            progressPercent: blocState.collectionLexicalItemProgressPercentMap[lexicalItem.id]!,
          );

          final permitted = blocState.permittedLexicalItemIds.contains(lexicalItem.id);

          return _DefaultStateLexicalItemModel(
            id: lexicalItem.id,
            title: lexicalItem.title,
            translation: lexicalItem.translation,
            progressModel: progressModel,
            permitted: permitted,
          );
        }).toBuiltList();

        return _DefaultState(
          collection: blocState.collection,
          collectionLexicalItemModels: collectionLexicalItemModels,
          knownLexicalItemSelectionPermitted: blocState.knownLexicalItemSelectionPermitted,
          lexicalItemAdditionPermitted: blocState.lexicalItemAdditionPermitted,
          collectionRepetitionProgresses: blocState.collectionRepetitionProgresses,
          unspecifiedLexicalItemExists: blocState.unspecifiedLexicalItemExists,
        );

      case bloc.KnownLexicalItemSelection():
        final unspecifiedCollectionLexicalItemModels =
            blocState.unspecifiedLexicalItems.map((lexicalItem) {
          final progressModel = LexicalItemProgressModelFactory().create(
            lexicalItem: lexicalItem,
            progressPercent: blocState.collectionLexicalItemProgressPercentMap[lexicalItem.id]!,
          );

          final selected = blocState.selectedLexicalItemIds.contains(lexicalItem.id);

          return _KnownLexicalItemSelectionLexicalItemModel(
            id: lexicalItem.id,
            title: lexicalItem.title,
            translation: lexicalItem.translation,
            progressModel: progressModel,
            selected: selected,
          );
        }).toBuiltList();

        return _KnownLexicalItemSelection(
          unspecifiedCollectionLexicalItemModels: unspecifiedCollectionLexicalItemModels,
        );

      case bloc.LexicalItemRemoval():
        final collectionLexicalItemModels = blocState.collectionLexicalItems.map((lexicalItem) {
          final progressModel = LexicalItemProgressModelFactory().create(
            lexicalItem: lexicalItem,
            progressPercent: blocState.collectionLexicalItemProgressPercentMap[lexicalItem.id]!,
          );

          final permitted = blocState.permittedLexicalItemIds.contains(lexicalItem.id);

          final selected = blocState.selectedLexicalItemIds.contains(lexicalItem.id);

          return _LexicalItemRemovalLexicalItemModel(
            id: lexicalItem.id,
            title: lexicalItem.title,
            translation: lexicalItem.translation,
            progressModel: progressModel,
            permitted: permitted,
            selected: selected,
          );
        }).toBuiltList();

        final allLexicalItemSelected =
            blocState.collectionLexicalItems.length == blocState.selectedLexicalItemIds.length;

        return _LexicalItemRemoval(
          collectionLexicalItemModels: collectionLexicalItemModels,
          allLexicalItemSelected: allLexicalItemSelected,
        );

      case bloc.CustomCollectionRenaming():
        final collectionLexicalItemModels = blocState.collectionLexicalItems.map((lexicalItem) {
          final progressModel = LexicalItemProgressModelFactory().create(
            lexicalItem: lexicalItem,
            progressPercent: blocState.collectionLexicalItemProgressPercentMap[lexicalItem.id]!,
          );

          final permitted = blocState.permittedLexicalItemIds.contains(lexicalItem.id);

          return _CustomCollectionRenamingLexicalItemModel(
            id: lexicalItem.id,
            title: lexicalItem.title,
            translation: lexicalItem.translation,
            progressModel: progressModel,
            permitted: permitted,
          );
        }).toBuiltList();

        return _CustomCollectionRenaming(
          collection: blocState.collection,
          collectionLexicalItemModels: collectionLexicalItemModels,
          knownLexicalItemSelectionPermitted: blocState.knownLexicalItemSelectionPermitted,
          lexicalItemAdditionPermitted: blocState.lexicalItemAdditionPermitted,
          collectionRepetitionProgresses: blocState.collectionRepetitionProgresses,
          unspecifiedLexicalItemExists: blocState.unspecifiedLexicalItemExists,
        );
    }
  }
}
